package simulator;

import java.util.LinkedList;
import java.util.List;

public class NetUtils {
	
	public Net createRingNet(int size, double infectedPercentage, double t1) {
		Net net = new Net();
		net.name = "Ring";
		Pc start = net.createPc("pc"+size);
		size--;
		Pc prev = start;
		while(size > 0){
			Pc current = net.createPc("pc"+size);
			prev.addConection(current);
			prev = current;
			size--;
		}
		prev.addConection(start);
		infectPcs(net, infectedPercentage, t1);
		return net;
	}
	
	public Net createAllConnectedNet(int size, double infectedPercentage, double t1) {
		Net net = new Net();
		net.name = "All connected";
		
		while(size > 0){
			net.createPc("pc"+size);
			size--;
		}
		for(Pc pc: net.pcs){
			for(Pc other: net.pcs){
				if(!pc.equals(other)){
					pc.addConection(other);
				}
			}
		}
		
		infectPcs(net, infectedPercentage, t1);
		
		return net;
	}
	
	public Net createRandomNet(int size, double infectedPercentage, double t1) {
		Net net = this.createAllConnectedNet(size, infectedPercentage, t1);
		net.name = "Random";
		double p = 0.42;
		for(Pc pc: net.pcs){
			for(Pc other: net.pcs){
				if(!pc.equals(other)){
					if(Math.random() < p) pc.delConection(other);
				}
			}
		}
		
		for(Pc pc : net.pcs) {
			if(pc.getConnectedQuantity() == 0 && net.getPcs().size() > 1) {
				for(Pc other : net.pcs) {
					if(!pc.getName().equals(other.getName()) && pc.getConnectedQuantity() == 0) {
						pc.addConection(other);
					}
				}
			}
		}
		
		infectPcs(net, infectedPercentage, t1);
		
		return net;
	}
	
	public void clearPcs(Net net) {
		for(Pc pc : net.getPcs()) {
			pc.setState(State.Sane);
			pc.setStateTimeLeft(0);
		}
	}
	
	public void infectPcs(Net net, double infectedPercentage, double t1) {
		int infectQuantity = (int) Math.ceil((net.getPcs().size() * infectedPercentage) / 100);
		List<Pc> pcs = new LinkedList<Pc>(net.getPcs());
		for(int i = 0; i < infectQuantity && i < pcs.size(); i++) {
			Pc pci = pcs.get(i);
			pci.setState(State.Infected);
			pci.setStateTimeLeft(t1);
		}
	}
	
}
